<template>
    <a-drawer
        :title="formData.id ? '编辑考试' : '增加考试'"
        :width="600"
        :visible="visible"
        :destroy-on-close="true"
        :footer-style="{ textAlign: 'right' }"
        @close="onClose"
    >
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="名称：" name="name">
                <a-input v-model:value="formData.name" placeholder="请输入名称" allow-clear />
            </a-form-item>
            <a-form-item label="试卷名称：" name="paperName">
                <a-input v-model:value="formData.paperName" placeholder="请输入试卷名称" allow-clear />
            </a-form-item>
            <a-form-item label="组卷方式（1：人工组卷；2：随机组卷）数据字典：PAPER_GEN_TYPE：" name="genType">
                <a-input v-model:value="formData.genType" placeholder="请输入组卷方式（1：人工组卷；2：随机组卷）数据字典：PAPER_GEN_TYPE" allow-clear />
            </a-form-item>
            <a-form-item label="阅卷方式（1：客观题；2：主观题；）数据字典：PAPER_MARK_TYPE：" name="markType">
                <a-input v-model:value="formData.markType" placeholder="请输入阅卷方式（1：客观题；2：主观题；）数据字典：PAPER_MARK_TYPE" allow-clear />
            </a-form-item>
            <a-form-item label="展示方式（1：整卷展示；3：单题展示；）数据字典：PAPER_SHOW_TYPE：" name="showType">
                <a-input v-model:value="formData.showType" placeholder="请输入展示方式（1：整卷展示；3：单题展示；）数据字典：PAPER_SHOW_TYPE" allow-clear />
            </a-form-item>
            <a-form-item label="登录方式（1：正常登录；2：免登录；）数据字典：LOGIN_TYPE：" name="loginType">
                <a-input v-model:value="formData.loginType" placeholder="请输入登录方式（1：正常登录；2：免登录；）数据字典：LOGIN_TYPE" allow-clear />
            </a-form-item>
            <a-form-item label="考试开始时间：" name="startTime">
                <a-date-picker v-model:value="formData.startTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择考试开始时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="考试结束时间：" name="endTime">
                <a-date-picker v-model:value="formData.endTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择考试结束时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="限制分钟（考试开始时间由用户第一次打开试卷时计时）：" name="limitMinute">
                <a-input v-model:value="formData.limitMinute" placeholder="请输入限制分钟（考试开始时间由用户第一次打开试卷时计时）" allow-clear />
            </a-form-item>
            <a-form-item label="阅卷开始时间：" name="markStartTime">
                <a-date-picker v-model:value="formData.markStartTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择阅卷开始时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="阅卷结束时间：" name="markEndTime">
                <a-date-picker v-model:value="formData.markEndTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择阅卷结束时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="阅卷状态（1：未阅卷；2：阅卷中；3：已阅卷；）：" name="markState">
                <a-input v-model:value="formData.markState" placeholder="请输入阅卷状态（1：未阅卷；2：阅卷中；3：已阅卷；）" allow-clear />
            </a-form-item>
            <a-form-item label="成绩查询状态（1：考试结束后；2：不公布；3：交卷后）：" name="scoreState">
                <a-input v-model:value="formData.scoreState" placeholder="请输入成绩查询状态（1：考试结束后；2：不公布；3：交卷后）" allow-clear />
            </a-form-item>
            <a-form-item label="排名状态（1：公布；2：不公布）：" name="rankState">
                <a-input v-model:value="formData.rankState" placeholder="请输入排名状态（1：公布；2：不公布）" allow-clear />
            </a-form-item>
            <a-form-item label="匿名阅卷状态（1：是；2：否）：" name="anonState">
                <a-input v-model:value="formData.anonState" placeholder="请输入匿名阅卷状态（1：是；2：否）" allow-clear />
            </a-form-item>
            <a-form-item label="及格分数：" name="passScore">
                <a-input v-model:value="formData.passScore" placeholder="请输入及格分数" allow-clear />
            </a-form-item>
            <a-form-item label="总分数：" name="totalScore">
                <a-input v-model:value="formData.totalScore" placeholder="请输入总分数" allow-clear />
            </a-form-item>
            <a-form-item label="防作弊（1：试题乱序；2：选项乱序；3：禁止考试中切屏；4：禁止浏览器调试）：" name="sxes">
                <a-input v-model:value="formData.sxes" placeholder="请输入防作弊（1：试题乱序；2：选项乱序；3：禁止考试中切屏；4：禁止浏览器调试）" allow-clear />
            </a-form-item>
            <a-form-item label="用户IDS：" name="userIds">
                <a-input v-model:value="formData.userIds" placeholder="请输入用户IDS" allow-clear />
            </a-form-item>
            <a-form-item label="机构IDS：" name="orgIds">
                <a-input v-model:value="formData.orgIds" placeholder="请输入机构IDS" allow-clear />
            </a-form-item>
            <a-form-item label="阅卷用户IDS：" name="markUserIds">
                <a-input v-model:value="formData.markUserIds" placeholder="请输入阅卷用户IDS" allow-clear />
            </a-form-item>
            <a-form-item label="重考次数：" name="retakeNum">
                <a-input v-model:value="formData.retakeNum" placeholder="请输入重考次数" allow-clear />
            </a-form-item>
            <a-form-item label="状态（0：删除；1：发布；2：暂停；）：" name="status">
                <a-input v-model:value="formData.status" placeholder="请输入状态（0：删除；1：发布；2：暂停；）" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
        </template>
    </a-drawer>
</template>

<script setup name="exmExamForm">
    import { cloneDeep } from 'lodash-es'
    import { required } from '@/utils/formRules'
    import exmExamApi from '@/api/biz/exmExamApi'
    // 抽屉状态
    const visible = ref(false)
    const emit = defineEmits({ successful: null })
    const formRef = ref()
    // 表单数据
    const formData = ref({})
    const submitLoading = ref(false)

    // 打开抽屉
    const onOpen = (record) => {
        visible.value = true
        if (record) {
            let recordData = cloneDeep(record)
            formData.value = Object.assign({}, recordData)
        }
    }
    // 关闭抽屉
    const onClose = () => {
        formRef.value.resetFields()
        formData.value = {}
        visible.value = false
    }
    // 默认要校验的
    const formRules = {
    }
    // 验证并提交数据
    const onSubmit = () => {
        formRef.value
            .validate()
            .then(() => {
                submitLoading.value = true
                const formDataParam = cloneDeep(formData.value)
                exmExamApi
                    .exmExamSubmitForm(formDataParam, formDataParam.id)
                    .then(() => {
                        onClose()
                        emit('successful')
                    })
                    .finally(() => {
                        submitLoading.value = false
                    })
            })
    }
    // 抛出函数
    defineExpose({
        onOpen
    })
</script>
